package com.example.dell.jakatra.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.classic.common.MultipleStatusView;
import com.example.dell.jakatra.NetManager;
import com.example.dell.jakatra.R;

/**
 * Created by liangjianwei on 2017/12/27.
 */

public abstract class AbsBaseActivity extends AppCompatActivity {

    private MultipleStatusView mMultipleStatusView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewGroupConfig(true, true);
        initLayout();
        initViews(savedInstanceState);

        NetManager.getInstance().setOnNetChangeListener(new NetManager.onNetChangeListsner() {
            @Override
            public void success() {
                if (mMultipleStatusView != null) {
                    mMultipleStatusView.showContent();
                }
            }

            @Override
            public void fail() {
                if (mMultipleStatusView != null) {
                    mMultipleStatusView.showError();
                }
            }
        });
    }

    public void showContent() {
        if (mMultipleStatusView != null) {
            mMultipleStatusView.showContent();
        }
    }

    private void initLayout() {
        if (isUseMultipleView) {
            RelativeLayout fatherView = (RelativeLayout) View.inflate(this, R.layout.activity_base, null);
            mMultipleStatusView = fatherView.findViewById(R.id.multiple_base);
            View childView = LayoutInflater.from(this).inflate(getLayoutId(), null);
            mMultipleStatusView.addView(childView);
            setContentView(fatherView);
        } else {
            setContentView(getLayoutId());
        }
        if (isShowLoadView && mMultipleStatusView != null) {
            mMultipleStatusView.showLoading();
        }
    }


    private boolean isUseMultipleView;
    private boolean isShowLoadView;


    public abstract int getLayoutId();

    public abstract void initViews(Bundle savedInstanceState);

    public void initViewGroupConfig(boolean isUseMultipleView, boolean isShowLoad) {
        this.isUseMultipleView = isUseMultipleView;
        this.isShowLoadView = isShowLoad;
    }
}
